/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.acceptance.util.worker.Req_087_Util;
import org.ourgrid.acceptance.util.worker.Req_094_Util;
import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;

@ReqTest(reqs="REQ087")
public class Req_087_Test extends WorkerAcceptanceTestCase {
	
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	private Req_094_Util req_094_Util = new Req_094_Util(getComponentContext());
	private Req_087_Util req_087_Util = new Req_087_Util(getComponentContext());
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		workerAcceptanceUtil.simulateAuthentication();
	}
	
	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector disabled and the public 
	 *    key property set to "publicKey1";
	 *    2. Pause the Worker with the public key "wrongPublicKey";
	 *           * Verify if the following warn message was logged: 
	 *    An unknown entity tried to pause the Worker. Only the local modules 
	 *    can perform this operation. Unknown entity public key: 
	 *    [wrongPublicKey].
	 *    3. Verify if the worker status is IDLE;
	 * 
	 */
	@ReqTest(test=" AT-087.1", reqs="REQ087")
	@Test public void test_at_087_1_PauseWorkerWithWrongPublicKey() throws Exception {
		// Start worker without idleness detector
		WorkerComponent component = req_004_Util.startWorker();
		
		// Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		// Pause the Worker with wrong public key - expect a warn to be logged
		req_087_Util.pauseWorkerByUnknownEntity(component, "wrongPublicKey");
		
		// Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	} 
	

	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector enabled;
	 *    2. Verify if the worker status is OWNER;
	 *    3. Pause the Worker;
	 *           * Verify if no message was logged
	 *           * Verify if the ControlClient received the operation succeed 
	 *             message
	 *    4. Verify if the worker status is OWNER;
	 * 
	 */
	@ReqTest(test=" AT-087.2", reqs="REQ087")
	@Test public void test_at_087_2_PauseWorkerWithIdlenessDetector() throws Exception {
		// Start worker with idleness detector
		WorkerComponent component = req_004_Util.startWorker(true);
		
		// Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
		
		// Pause the Worker
		req_087_Util.pauseOwnerWorker(component);
		
		// Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
	}
	
	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector disabled;
	 *    2. Verify if the worker status is IDLE;
	 *    3. Pause the Worker;
	 *           * Verify if the info message was logged: 
	 *           "Worker has been PAUSED."
	 *           * Verify if the debug message was logged: 
	 *           "Status changed from IDLE to OWNER."
	 *           * Verify if the ControlClient received the operation succeed 
	 *           message
	 *    4. Verify if the worker status is OWNER;
	 * 
	 */
	@ReqTest(test=" AT-087.3", reqs="REQ087")
	@Test public void test_at_087_3_PauseWorkerWithoutIdlenessDetector() throws Exception {
		// Start the worker without idleness detector
		WorkerComponent component = req_004_Util.startWorker();
		
		// Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
		
		// Pause the Worker
		req_087_Util.pauseIdleWorker(component);
		
		// Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
	}
}